﻿using System;
using System.Threading.Tasks;
using FutureApp.Media.Dto;
using Abp.Domain.Repositories;
using Abp.AutoMapper;
using System.Linq;

namespace FutureApp.Media
{
    /// <AppService/>
    public class PictureAppService : FutureAppAppServiceBase, IPictureAppService
    {
        private IRepository<Picture, Guid> PictureRepository { get; }

        /// <constructor/>
        public PictureAppService(IRepository<Picture, Guid> pictureRepository)
        {
            this.PictureRepository = pictureRepository;
        }

        /// <constructor/>
        public async Task<PictureEntityDto> CreatePicture(CreatePictureInput input)
        {
            var entity = input.MapTo<Picture>();
            entity.IsDeleted = false;
            return (await this.PictureRepository.InsertAsync(entity)).MapTo<PictureEntityDto>();
        }

        /// <constructor/>
        public async Task<PictureEntityDto> GetPicture(Guid id)
        {
            var entity = await this.PictureRepository.GetAsync(id);
            return entity.MapTo<PictureEntityDto>();
        }

        /// <constructor/>
        public PictureEntityDto GetPictureByMd5(string md5)
        {
            var entity = this.PictureRepository.GetAll().FirstOrDefault(e => e.Md5 == md5);
            return entity.MapTo<PictureEntityDto>();
        }

        /// <constructor/>
        public async Task<bool> IsExistPicture(Guid id)
        {
            return 0 != await this.PictureRepository.CountAsync(e => e.Id == id);
        }
    }
}
